package arrays;

public class RotateImage {

	/**
	 */
	public static void main(String[] args) {

		int[][] image = new int[][] { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },
				{ 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 },
				{ 21, 22, 23, 24, 25 } };

		printImage(image);
		
		rotateImage(image);
		
		System.out.println();

		printImage(image);
		
		rotateImage(image);
		
		System.out.println();

		printImage(image);
	}

	private static void rotateImage(int[][] image) {
		
		for(int i=0; i<image.length/2; i++) {
			int first = i;
			int last = image.length - 1 - i;
			for(int j=i; j<image.length - 1 - i; j++) {
				int offset = j-i;
				int tmp = image[first][j];
				image[first][j] = image[last-offset][first];
				image[last-offset][first] = image[last][last-offset];
				image[last][last-offset] = image[j][last];
				image[j][last] = tmp;
			}
		}
	}

	private static void printImage(int[][] image) {
		
		for (int i = 0; i < image.length; i++) {
			for (int j = 0; j < image[i].length; j++) {
				System.out.print(image[i][j] + " ");
			}
			System.out.println();
		}
		
	}

}
